<?php
include_once '../__global.php';
include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_user.php');

define('NEED_VERIFYCODE', 'needvcode');

class aj_login extends ml_controller {
    private $account;
    private $pwd;
    private $remember;
    private $nickOrEmail = 0;              //0-nick 1-email
    private $vcode;
    
    function initParam() {
        $this->account = $this->input('account', 'P', '');
        $this->pwd = $this->input('pwd', 'P', '');
        $this->remember = $this->input('remember', 'P', 1);  //自动登录，默认记
        $this->vcode = $this->input('vcode', 'P', '');
    }
    
    function checkParam() {
        $this->check_referer();
        if (empty($this->account)) {
            $this->api_output(ML_RCODE_PARAM, '' , '邮箱不能为空');
        }
        if (empty($this->pwd)) {
            $this->api_output(ML_RCODE_PARAM,'', '密码不能为空');
        }
        
        if(filter_var($this->account , FILTER_VALIDATE_EMAIL) == false)
            $this->api_output(ML_RCODE_PARAM ,'', '邮箱不正确');
        
        $len = strlen($this->account);
        if($len < ML_DATARULE_USER_EMAIL_MINLEN || $len > ML_DATARULE_USER_EMAIL_MAXLEN)
            $this->api_output(ML_RCODE_PARAM , '' , '邮箱不正确');
        
        $this->nickOrEmail = 1;
        
        if(!preg_match(ML_DATARULE_USER_PASSWORD_PREG , $this->pwd))
            $this->api_output(ML_RCODE_PARAM , '' , '密码错误');
        
        if (!$this->check_permission(ML_PERMISSION_LOGOUT_ONLY)) {
            $this->loginProxy(ml_logout);
        }
        
    }
    
    public function main() {
    
        /**
         * 
         */
        $oSession = $this->getSession();
        $vcode_sess = $oSession->getVal('verify_default');
        $oSession->unregister('verify_default');
        
        $count = $oSession->getVal('login_fail_count');
        if (empty($count)) $count = 0;
        
        $oSession->save();
        
        if(3 < $count && !ml_tool_securecode::check_validate_code($this->vcode , $vcode_sess))
            $this->api_output(ML_RCODE_PARAM , array('result' => 'vcode') , '');
        
        $oAccount = new ml_model_dbUserAccount();
        $re = $oAccount->checkPassword($this->pwd, $this->account, $this->nickOrEmail);   //根据邮箱
        
        $rs = $oAccount->get_data();
                
        if (!$re || empty($rs)) {
            
            //session存储失败次数
            $count++;
            $oSession->setVal('login_fail_count',$count);
            $oSession->save();
            
            if (2 < $count) $data = NEED_VERIFYCODE;
            
            $this->api_output( ML_RCODE_FAIL, array('result' => $data) , '昵称邮箱不存在或密码错误');
        }
 
        
        
        if ($rs['status'] == ML_USERSTATUS_KILL) {
            $this->api_output(ML_RCODE_FAIL, '', '用户被封杀');
        }
        
        $this->loginProxy(ml_login, $rs);
        $oSession->setVal('login_fail_count', 0);
        $oSession->save();
        
        if($this->remember){        
            //设置cookie
            $this->_login->cookie_remember($rs['uid'], $rs['email']);
        }
        
        $data = array();
        $data['nickname'] = $this->__visitor['nickname'];
        $data['uid'] = $this->__visitor['uid'];
        $data['headPic'] = ml_tool_picid::uid2portrait($this->__visitor['uid'], 'tny');
        
        ml_tool_actionlog::login($rs['uid'] , 0);
        
        $this->api_output( ML_RCODE_SUCC, $data);
        
        
    }
    
}

new aj_login();